/*     */ package com.newcrm.manager;
/*     */ 
/*     */ import com.newcrm.bean.CustomerSearchCondition;
/*     */ import com.newcrm.bean.User;
/*     */ import com.newcrm.db.ConnectionManager;
/*     */ import com.newcrm.db.CustomerFieldManager;
/*     */ import com.newcrm.db.CustomerManager;
/*     */ import com.newcrm.db.ExportTaskManager;
/*     */ import com.newcrm.db.UserManager;
/*     */ import com.newcrm.util.ExcelUtil;
/*     */ import com.newcrm.util.Util;
/*     */ import java.io.File;
/*     */ import java.io.FileOutputStream;
/*     */ import java.sql.Connection;
/*     */ import java.sql.SQLException;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Date;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import org.apache.commons.dbutils.DbUtils;
/*     */ import org.apache.log4j.Logger;
/*     */ 
/*     */ public class ExportManager
/*     */ {
/*  35 */   private static Logger logger = Logger.getLogger(ExportManager.class
/*  36 */     .getName());
/*     */ 
/*  38 */   private static ExportManager instance = null;
/*     */   private static final int STATUS_NONE = -1;
/*     */   private static final int STATUS_BEGIN = 0;
/*     */   private static final int STATUS_FINISH = 1;
/*  45 */   private List<Map<String, Object>> list = new ArrayList();
/*  46 */   private List<User> users = null;
/*     */ 
/*  49 */   private String folder = null;
/*     */ 
/*  51 */   public int currentExportTaskId = 0;
/*  52 */   public int exportDataNum = 0;
/*     */ 
/*     */   private ExportManager(String folder) {
/*  55 */     this.folder = folder;
/*     */   }
/*     */ 
/*     */   public static ExportManager getInstance(String folder) {
/*  59 */     if (instance == null) {
/*  60 */       instance = new ExportManager(folder);
/*     */     }
/*  62 */     return instance;
/*     */   }
/*     */ 
/*     */   public void addTask(User user, CustomerSearchCondition searchCondition, String fileName, int totalNum)
/*     */   {
/*  76 */     if (isExist(searchCondition)) {
/*  77 */       return;
/*     */     }
/*     */ 
/*  81 */     ExportTaskManager manager = new ExportTaskManager();
/*     */     try {
/*  83 */       int id = manager.add(user.companyId, Util.date2Str2(new Date()), 
/*  84 */         totalNum, user, searchCondition, fileName, user.userId);
/*     */ 
/*  86 */       Map map = new HashMap();
/*  87 */       map.put("user", user);
/*  88 */       map.put("searchCondition", searchCondition);
/*  89 */       map.put("fileName", fileName);
/*  90 */       map.put("isFinish", Integer.valueOf(-1));
/*  91 */       map.put("id", Integer.valueOf(id));
/*  92 */       this.list.add(map);
/*     */     } catch (Exception e) {
/*  94 */       logger.warn("get export task id failed", e);
/*     */     }
/*     */ 
/*  97 */     exportData();
/*     */   }
/*     */ 
/*     */   public boolean isExist(CustomerSearchCondition searchCondition)
/*     */   {
/* 107 */     if ((this.list == null) || (this.list.size() == 0)) {
/* 108 */       return false;
/*     */     }
/*     */ 
/* 111 */     for (Map item : this.list) {
/* 112 */       if (searchCondition.equals(item.get("searchCondition"))) {
/* 113 */         return true;
/*     */       }
/*     */     }
/* 116 */     return false;
/*     */   }
/*     */ 
/*     */   public void clearTask(int exportTaskId)
/*     */   {
/* 125 */     if ((this.list == null) || (this.list.size() == 0)) {
/* 126 */       return;
/*     */     }
/* 128 */     for (int i = 0; i < this.list.size(); i++) {
/* 129 */       Map map = (Map)this.list.get(i);
/* 130 */       if (exportTaskId == ((Integer)map.get("id")).intValue()) {
/* 131 */         if (exportTaskId == this.currentExportTaskId) {
/* 132 */           this.currentExportTaskId = 0;
/* 133 */           this.exportDataNum = 0;
/*     */         }
/* 135 */         this.list.remove(i);
/* 136 */         return;
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public boolean isExistTaskId(int taskId)
/*     */   {
/* 147 */     if ((this.list == null) || (this.list.size() == 0)) {
/* 148 */       return false;
/*     */     }
/* 150 */     for (int i = 0; i < this.list.size(); i++) {
/* 151 */       Map map = (Map)this.list.get(i);
/* 152 */       if (taskId == ((Integer)map.get("id")).intValue()) {
/* 153 */         return true;
/*     */       }
/*     */     }
/* 156 */     return false;
/*     */   }
/*     */ 
/*     */   private void exportData()
/*     */   {
/* 163 */     if ((this.list == null) || (this.list.size() == 0)) {
/* 164 */       logger.warn("没有需要导出的任务了");
/* 165 */       return;
/*     */     }
/*     */ 
/* 168 */     Map map = (Map)this.list.get(0);
/*     */ 
/* 170 */     if (1 == ((Integer)map.get("isFinish")).intValue()) {
/* 171 */       logger.warn("第一个任务已经搜索完毕了，从任务队列中删除，开始处理队列中的下一个导出任务");
/* 172 */       this.list.remove(0);
/* 173 */       this.currentExportTaskId = 0;
/* 174 */       this.exportDataNum = 0;
/* 175 */       exportData();
/* 176 */       return;
/*     */     }
/*     */ 
/* 180 */     if (((Integer)map.get("isFinish")).intValue() == 0) {
/* 181 */       logger.warn("当前有任务，正在导出，等待该任务处理完毕后，再处理新的任务");
/* 182 */       return;
/*     */     }
/*     */ 
/* 186 */     logger.warn("开始导出数据");
/* 187 */     this.currentExportTaskId = 0;
/* 188 */     this.exportDataNum = 0;
/* 189 */     map.put("isFinish", Integer.valueOf(0));
/* 190 */     export((User)map.get("user"), 
/* 191 */       (CustomerSearchCondition)map.get("searchCondition"), 
/* 192 */       this.folder + File.separator + (String)map.get("fileName"), 
/* 193 */       ((Integer)map.get("id")).intValue());
/*     */   }
/*     */ 
/*     */   private void export(User user, CustomerSearchCondition searchCondition, String filePath, int exportTaskId)
/*     */   {
/* 205 */     List fields = null;
/* 206 */     CustomerFieldManager customerFiledManager = new CustomerFieldManager();
/*     */     try {
/* 208 */       fields = customerFiledManager.listAll(user.companyId);
/*     */     } catch (Exception e) {
/* 210 */       logger.warn("get all customer ext field failed", e);
/*     */     }
/*     */ 
/* 214 */     UserManager userManager = new UserManager();
/*     */     try {
/* 216 */       this.users = userManager.getCompanyUsers(user.companyId, null);
/*     */     } catch (Exception e) {
/* 218 */       logger.warn("get company user field failed", e);
/*     */     }
/*     */ 
/* 222 */     List result = new ArrayList();
/* 223 */     CustomerManager manager = new CustomerManager();
/* 224 */     ExportTaskManager exportTaskManager = new ExportTaskManager();
/*     */ 
/* 226 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 227 */     if (conn == null) {
/* 228 */       return;
/*     */     }
/*     */ 
/* 231 */     int offset = 0;
/*     */     try {
/* 233 */       result = manager.getList(conn, searchCondition, offset, 
/* 234 */         10, user, true);
/*     */ 
/* 236 */       if ((result == null) || (result.size() == 0)) {
/* 237 */         logger.warn("搜索结果是0， 处理下一个导出任务");
/* 238 */         exportTaskManager.setFinish(conn, exportTaskId, 0);
/* 239 */         clearTask(exportTaskId);
/* 240 */         exportData();
/* 241 */         return;
/*     */       }
/*     */ 
/* 245 */       searchExtInfo(conn, result, user.companyId);
/*     */ 
/* 247 */       List heads = new ArrayList();
/* 248 */       List headsKey = new ArrayList();
/* 249 */       heads.add("名称");
/* 250 */       headsKey.add("Name");
/* 251 */       heads.add("客户分类");
/* 252 */       headsKey.add("CategoryName");
/* 253 */       heads.add("所属员工");
/* 254 */       headsKey.add("PermissionsName");
/*     */ 
/* 256 */       heads.add("更新时间");
/* 257 */       headsKey.add("UpdateDate");
/*     */ 
/* 261 */       heads.add("联系人");
/* 262 */       headsKey.add("people");
/* 263 */       heads.add("职位");
/* 264 */       headsKey.add("title");
/*     */ 
/* 267 */       heads.add("联系人电话");
/* 268 */       headsKey.add("peopleExtInfo_1");
/*     */ 
/* 271 */       heads.add("联系人邮箱");
/* 272 */       headsKey.add("peopleExtInfo_2");
/*     */ 
/* 275 */       heads.add("联系人QQ");
/* 276 */       headsKey.add("peopleExtInfo_3");
/*     */ 
/* 279 */       for (Object fieldO : fields) {
            Map field = (Map)fieldO;
/* 280 */         int fieldId = Util.str2Int(field.get("CustomerFieldId"));
/* 281 */         if (fieldId >= 99)
/*     */         {
/* 285 */           int fieldType = Util.str2Int(field.get("FieldType"));
/* 286 */           if (isShowField(1, fieldType)) {
/* 287 */             heads.add((String)field.get("CustomerFieldName"));
/* 288 */             headsKey.add("peopleExtInfo_" + fieldId);
/*     */           }
/*     */         }
/*     */       }
/*     */ 
/* 293 */       heads.add("公司电话");
/* 294 */       headsKey.add("companyExtInfo_1");
/*     */ 
/* 297 */       heads.add("公司邮箱");
/* 298 */       headsKey.add("companyExtInfo_2");
/*     */ 
/* 301 */       heads.add("公司QQ");
/* 302 */       headsKey.add("companyExtInfo_3");
/*     */ 
/* 305 */       heads.add("网站");
/* 306 */       headsKey.add("companyExtInfo_4");
/*     */ 
/* 309 */       heads.add("地址");
/* 310 */       headsKey.add("companyExtInfo_6");
/*     */ 
/* 313 */       heads.add("简介");
/* 314 */       headsKey.add("Note");
/*     */ 
/* 317 */       for (Object fieldO : fields) {
            Map field = (Map)fieldO;
/* 318 */         int fieldId = Util.str2Int(field.get("CustomerFieldId"));
/* 319 */         if (fieldId >= 99)
/*     */         {
/* 323 */           int fieldType = Util.str2Int(field.get("FieldType"));
/* 324 */           if (isShowField(0, fieldType)) {
/* 325 */             heads.add((String)field.get("CustomerFieldName"));
/* 326 */             headsKey.add("companyExtInfo_" + fieldId);
/*     */           }
/*     */         }
/*     */       }
/* 330 */       FileOutputStream out = new FileOutputStream(filePath);
/*     */ 
/* 332 */       ExcelUtil.export("客户信息", (String[])heads.toArray(new String[0]), 
/* 333 */         (String[])headsKey.toArray(new String[0]), result, null, out);
/*     */ 
/* 335 */       exportTaskManager.updateExportNum(conn, exportTaskId, offset + 
/* 336 */         result.size());
/*     */ 
/* 338 */       this.currentExportTaskId = exportTaskId;
/* 339 */       this.exportDataNum = (offset + result.size());
/*     */ 
/* 341 */       while ((result != null) && (result.size() >= 10) && 
/* 342 */         (isExistTaskId(exportTaskId))) {
/* 343 */         offset += 10;
/* 344 */         result = manager.getList(conn, searchCondition, offset, 
/* 345 */           10, user, true);
/*     */ 
/* 347 */         searchExtInfo(conn, result, user.companyId);
/*     */ 
/* 350 */         logger.warn("往文件中，追加了数据" + result.size() + " 条");
/*     */ 
/* 352 */         ExcelUtil.addData(filePath, (String[])headsKey.toArray(new String[0]), 
/* 353 */           result, null, offset);
/*     */ 
/* 355 */         exportTaskManager.updateExportNum(conn, exportTaskId, offset + 
/* 356 */           result.size());
/*     */ 
/* 358 */         this.currentExportTaskId = exportTaskId;
/* 359 */         this.exportDataNum = (offset + result.size());
/*     */       }
/*     */ 
/* 362 */       exportTaskManager.setFinish(conn, exportTaskId, 
/* 363 */         offset + result.size());
/*     */ 
/* 365 */       clearTask(exportTaskId);
/*     */ 
/* 368 */       exportData();
/*     */     }
/*     */     catch (Exception e) {
/* 371 */       logger.warn("export customers info failed", e);
/*     */       try
/*     */       {
/* 374 */         DbUtils.close(conn);
/*     */       } catch (SQLException se) {
/* 376 */         logger.warn("close conn failed", se);
/*     */       }
/*     */     }
/*     */     finally
/*     */     {
/*     */       try
/*     */       {
/* 374 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 376 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   private void searchExtInfo(Connection conn, List<Map<String, Object>> result, int companyId)
/*     */   {
/* 390 */     Map customerIdIndex = new HashMap();
/* 391 */     StringBuffer customerIds = new StringBuffer();
/*     */ 
/* 393 */     for (int i = 0; i < result.size(); i++) {
/* 394 */       Map customer = (Map)result.get(i);
/*     */ 
/* 397 */       if (Util.str2Int(customer.get("Visible")) == 1) {
/* 398 */         customer.put("PermissionsName", "公共数据");
/*     */       }
/* 400 */       else if (customer.get("PermissionsId") == null) {
/* 401 */         customer.put("PermissionsName", "公共数据");
/*     */       } else {
/* 403 */         String currentPermissionsId = 
/* 404 */           (String)customer
/* 404 */           .get("PermissionsId");
/* 405 */         customer.put("PermissionsName", 
/* 406 */           getUserName(companyId, currentPermissionsId));
/*     */       }
/*     */ 
/* 411 */       customerIdIndex.put(Integer.valueOf(Util.str2Int(customer.get("CustomerId"))), Integer.valueOf(i));
/* 412 */       if (customerIds.length() > 0) {
/* 413 */         customerIds.append(",");
/*     */       }
/*     */ 
/* 416 */       customerIds.append(customer.get("CustomerId").toString());
/*     */     }
/*     */ 
/* 420 */     if (customerIds.length() > 0)
/* 421 */       getCustomerExtInfo(conn, customerIds.toString(), result, 
/* 422 */         customerIdIndex);
/*     */   }
/*     */ 
/*     */   private String getUserName(int companyId, String userId)
/*     */   {
/* 435 */     if (this.users == null) {
/* 436 */       return null;
/*     */     }
/* 438 */     for (User u : this.users) {
/* 439 */       if (u.userId.equals(userId)) {
/* 440 */         return u.name;
/*     */       }
/*     */     }
/* 443 */     return null;
/*     */   }
/*     */ 
/*     */   private void getCustomerExtInfo(Connection conn, String ids, List<Map<String, Object>> result, Map<Integer, Integer> customerIdIndex)
/*     */   {
/* 457 */     CustomerManager manager = new CustomerManager();
/*     */     try
/*     */     {
/* 461 */       List extInfos = manager.getCustomerAllExtInfo(
/* 462 */         conn, ids);
/* 463 */       fillExtInfo("company", result, extInfos, customerIdIndex);
/*     */     } catch (Exception e) {
/* 465 */       logger.warn("get customers ext info failed", e);
/*     */     }
/*     */ 
/*     */     try
/*     */     {
/* 470 */       List peopleExtInfos = manager.getPeoplesName(
/* 471 */         conn, ids);
/* 472 */       fillPeopleNameInfo(result, peopleExtInfos, customerIdIndex);
/*     */     } catch (Exception e) {
/* 474 */       logger.warn("get customers ext info failed", e);
/*     */     }
/*     */ 
/*     */     try
/*     */     {
/* 479 */       List peopleExtInfos = manager
/* 480 */         .getPeopleAllExtInfo(conn, ids);
/* 481 */       fillExtInfo("people", result, peopleExtInfos, customerIdIndex);
/*     */     } catch (Exception e) {
/* 483 */       logger.warn("get customers ext info failed", e);
/*     */     }
/*     */   }
/*     */ 
/*     */   private void fillPeopleNameInfo(List<Map<String, Object>> result, List<Map<String, Object>> extInfos, Map<Integer, Integer> customerIdIndex)
/*     */   {
/* 498 */     if ((extInfos == null) || (extInfos.size() == 0)) {
/* 499 */       return;
/*     */     }
/* 501 */     for (Map extInfo : extInfos)
/*     */     {
/* 503 */       int id = Util.str2Int(extInfo.get("Id"));
/* 504 */       Integer idx = (Integer)customerIdIndex.get(Integer.valueOf(id));
/*     */ 
/* 506 */       if (idx != null)
/*     */       {
/* 510 */         if (idx.intValue() >= 0)
/*     */         {
/* 514 */           if (idx.intValue() <= result.size())
/*     */           {
/* 519 */             Map customer = (Map)result.get(idx.intValue());
/* 520 */             if (customer != null)
/*     */             {
/* 524 */               String name = (String)extInfo.get("Name");
/* 525 */               String title = (String)extInfo.get("Title");
/* 526 */               if ((name != null) && (name.trim().length() != 0))
/*     */               {
/* 531 */                 String oldValue = (String)customer.get("people");
/* 532 */                 if ((oldValue != null) && (oldValue.trim().length() > 0)) {
/* 533 */                   name = oldValue + " " + name;
/*     */                 }
/* 535 */                 customer.put("people", name);
/*     */ 
/* 538 */                 if ((title != null) && (title.trim().length() != 0))
/*     */                 {
/* 541 */                   oldValue = (String)customer.get("title");
/* 542 */                   if ((oldValue != null) && (oldValue.trim().length() > 0)) {
/* 543 */                     title = oldValue + " " + title;
/*     */                   }
/* 545 */                   customer.put("title", title);
/*     */                 }
/*     */               }
/*     */             }
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   private void fillExtInfo(String prefix, List<Map<String, Object>> result, List<Map<String, Object>> extInfos, Map<Integer, Integer> customerIdIndex)
/*     */   {
/* 559 */     if ((extInfos == null) || (extInfos.size() == 0)) {
/* 560 */       return;
/*     */     }
/* 562 */     String extPrefix = prefix;
/* 563 */     for (Map extInfo : extInfos)
/*     */     {
/* 565 */       int id = Util.str2Int(extInfo.get("Id"));
/* 566 */       Integer idx = (Integer)customerIdIndex.get(Integer.valueOf(id));
/*     */ 
/* 568 */       if (idx != null)
/*     */       {
/* 572 */         if (idx.intValue() >= 0)
/*     */         {
/* 576 */           if (idx.intValue() <= result.size())
/*     */           {
/* 581 */             Map customer = (Map)result.get(idx.intValue());
/* 582 */             if (customer != null)
/*     */             {
/* 587 */               if (("company".equals(extPrefix)) && 
/* 588 */                 (customer.get("Type") != null)) {
/* 589 */                 int customerType = ((Integer)customer.get("Type")).intValue();
/* 590 */                 if (1 == customerType) {
/* 591 */                   extPrefix = "people";
/*     */                 }
/*     */ 
/*     */               }
/*     */ 
/* 605 */               int type = Util.str2Int(extInfo.get("Type"));
/* 606 */               String value = (String)extInfo.get("Info1");
/*     */ 
/* 610 */               if ((value != null) && (value.trim().length() != 0))
/*     */               {
/* 614 */                 String oldValue = (String)customer.get(extPrefix + "ExtInfo_" + 
/* 615 */                   type);
/* 616 */                 if ((oldValue != null) && (oldValue.trim().length() > 0)) {
/* 617 */                   value = oldValue + " " + value;
/*     */                 }
/* 619 */                 customer.put(extPrefix + "ExtInfo_" + type, value);
/*     */               }
/*     */             }
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   private boolean isShowField(int customerType, int fieldType)
/*     */   {
/* 632 */     if (customerType == -1) {
/* 633 */       return true;
/*     */     }
/*     */ 
/* 636 */     if (fieldType == 0) {
/* 637 */       return true;
/*     */     }
/*     */ 
/* 641 */     if (customerType == 1) {
/* 642 */       return fieldType == 2;
/*     */     }
/*     */ 
/* 645 */     if (customerType == 0) {
/* 646 */       return fieldType == 1;
/*     */     }
/* 648 */     return false;
/*     */   }
/*     */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.manager.ExportManager
 * JD-Core Version:    0.6.1
 */